<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<%@ include file="/common/import.jsp" %>
<link href="${ctx}/css/hall.css" rel="stylesheet">
<body>
<%@ include file="/common/halltop.jsp" %>
<div class="wrap" style="height:500px;">
<div style="margin:10px;width:28%;height:300px;float:left;background:#ffaaff">
标的图【${loginUser.userEname}】
</div>
<div style="padding:10px;width:40%;float:left;">
	<table class="info" cellspacing="1" cellpadding="0">
		<tr>
			<th>标的名称：</th>
			<td colspan=3>${item.itemName }</td>
		</tr>
		<tr>
			<th>起始价格：</th>
			<td>${item.firstValuePre }</td>
			<th>加价幅度：</th>
			<td>${item.incrementValue }</td>
		</tr>
		<tr>
			<th>最新价格：</th>
			<td><font  id='last_value'>${price.lastValue }</font> (${item.priceUnit})${item.currency}</td>
			<th>最新报价人：</th>
			<td id="user_name">${price.userName }</td>
		</tr>
		<tr>
			<th>报价次数：</th>
			<td id="bid_num">${price.bidTimes }</td>
			<th>最新报价时间：</th>
			<td id="last_bid_time">${price.lastBidTime }</td>
		</tr>		
		<tr>
			<th>所在期：</th>
			<td><font id="itemStatus"></font></td>
			<th>剩余时间：</th>
			<td><font id="spareTime"></font></td>
		</tr>
	</table>
	
	<c:if test="${loginUser.userEname == price.userName && price.ifAddPrice=='F'}">	
		<center id="baojiaBtn" style="margin:10px;">
					加价：<input id="myPrice" name="myPrice" /> 
					<a id="btnAddPrice" class="easyui-linkbutton"  iconCls="icon-add" onclick="baojia();">加价</a>
		</center>
	</c:if>
	<c:if test="${loginUser.userEname != price.userName}">	
		<center id="baojiaBtn" style="margin:10px;">
					最高报价人正在加价过程。。。
		</center>
	</c:if>
	
	</div>
	<div style="padding:2px;width:25%;height:300px;float:right;">
	
	<table id="info_table" class="info" cellspacing="1" cellpadding="0">
		<tr>
			<th>报价人</th>
			<th>报价价格</th>	
			<th>报价时间</th>
		</tr>
	</table>
	<script type="text/template" id="info_html">
	<? for ( var i = 0; i < rows.length; i++) {?>
		<tr>
			<td><?=rows[i].userName?></td>
			<td><?=rows[i].bidValue?></td>
			<td><?=rows[i].bidTime?></td>
		</tr>
	<?}?>  		
	</script>
	</div>
</div>
<%@ include file="/common/hallbottom.jsp" %>
</body>
</html>
<script>
var myLevel = "${myLevel}";//当前登录人针对此标的的行权级别
var itemId= "${item.itemId}";
var decimalCount = "${item.decimalCount}";
var currentBidTime="0";
///如果lastValue为空取firstValue;
var lastValue = "${price.lastValue}";//${price.lastValue}==0? ${price.firstValue} : ${price.lastValue};
if(lastValue=="0"|| lastValue=="")lastValue = "${price.firstValue}";
var incrementGroup ="${item.incrementGroup}";
var incrementValue ="${item.incrementValue}";
var increment=getIncrement();

function getIncrement(){
	var increment ;
	if(incrementValue.indexOf(',')==-1){
		increment = incrementValue;
	}else{
		increment = 300;
	}
	return increment;
}

function bidGet(){
	var bidGetUrl = ctx+spaceDomain+'hall/bidGet';
	var bidNum=$("#bid_num").html();
	$.ajax({
		   type:'get',
		   url:bidGetUrl,
		   data:{itemId:itemId,bidNum:currentBidTime},
		   dataType:'json',
		   success: function(myData){
			    $("#spareTime").html(spareTxt(myData.spareTime));
			    $("#itemStatus").html(itemStatusTxt(myData.itemStatus))
			    
			   if(myData.ischange){
				   //incrementValue = getNewincrementValue(myData.price.lastValue,{item.increntGroup},{item.increntValue});
				   $("#last_value").html(formatNumber(myData.price.lastValue,decimalCount,0));
				   lastValue = myData.price.lastValue;
				   $("#bid_num").html(myData.price.bidTimes);
				   currentBidTime = myData.price.bidTimes;
				   $("#last_bid_time").html(myData.price.lastBidTime);
				   
				   //加载报价记录
				   deleteAllRow("info_table");
				   var myDataBaoJia = new Object();
				   myDataBaoJia.rows = myData.list;
				   var htmlData = baidu.template('info_html',eval(myDataBaoJia));
				   if(htmlData){
					   $("#info_table").append(htmlData);
			   	   }
				   
				   
			   }
		  }
	});	
}


/**
* 输出long 的时间长度    如xx天xx小时xx分xx秒
*/
function spareTxt(spare){
	var result = "";
	if(spare>0){
    spareDays = Math.floor(spare / (24 * 60 * 60 * 1000));
    spareHouse= Math.floor((spare - spareDays * (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));
    spareMinutes  = Math.floor((spare - spareDays * (24 * 60 * 60 * 1000) - spareHouse * 60 * 60 * 1000) / (60 * 1000));
    spareSeconds  = Math.floor((spare - spareDays * (24 * 60 * 60 * 1000) - spareMinutes * 60 * 1000 - spareHouse * 60 * 60 * 1000) / 1000);
     result = spareDays > 0 ? spareDays + "天" : "";
    result += spareHouse > 0 ? spareHouse + "小时" : "";
    result += spareMinutes > 0 ? spareMinutes + "分" : "";    
    result += spareSeconds > 0 ? spareSeconds + "秒" : "";   
     return result;
    }
   return "超时错误";
   
}
function itemStatusTxt(status){
	//alert(status);
	var xx = "--";
	if(status=="A")xx = "竞价未开始";
	if(status=="B")xx = "自由竞价期";
	if(status=="C")xx = "限时竞价期";
	if(status=="D")xx = "加价期";
	if(status=="E")xx = "行权期";
	if(status=="9")xx = "竞价结束";
	return xx;
}

function baojia(n){
	//alert(n);
	var myPrice = $("#myPrice").val();
	if(!myPrice){
		alret("请输入价格");
		return;
	}
	
	if(confirm("你确实要加价到："+myPrice+"吗?")){
		var bidSendUrl = ctx+spaceDomain+'hall/bidAdd';
		$.ajax({
			   type:'get',
			   url:bidSendUrl,
			   data:{itemId:itemId,myPrice:myPrice},
			   dataType:'json',
			   success: function(myData){
					if(myData.flag=="T"){
						alert("加价成功");
						$("#baojiaBtn").hide();
					}else{
						alert(myData.msg);
					}
			  }
		});	
	}
}

function setButton(){
	$("#b1").html(increment*1);
	$("#b2").html(increment*2);
	$("#b3").html(increment*3);
	$("#b4").html(increment*4);
	$("#b5").html(increment*5);	
}

$(function(){
	setButton();
	setInterval(bidGet,2000);
});

/**
* 将数值四舍五入后格式化.
*
* @param num 数值(Number或者String)
* @param cent 要保留的小数位(Number)
* @param isThousand 是否需要千分位 0:不需要,1:需要(数值类型);
* @return 格式的字符串,如'1,234,567.45'
* @type String
*/
function formatNumber(num,cent,isThousand) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))//检查传入数值为数值类型.
     num = "0";
if(isNaN(cent))//确保传入小数位为数值型数值.
cent = 0;
cent = parseInt(cent);
cent = Math.abs(cent);//求出小数位数,确保为正整数.
if(isNaN(isThousand))//确保传入是否需要千分位为数值类型.
isThousand = 0;
isThousand = parseInt(isThousand);
if(isThousand < 0)
isThousand = 0;
if(isThousand >=1) //确保传入的数值只为0或1
isThousand = 1;
    sign = (num == (num = Math.abs(num)));//获取符号(正/负数)
//Math.floor:返回小于等于其数值参数的最大整数
    num = Math.floor(num*Math.pow(10,cent)+0.50000000001);//把指定的小数位先转换成整数.多余的小数位四舍五入.
    cents = num%Math.pow(10,cent); //求出小数位数值.
    num = Math.floor(num/Math.pow(10,cent)).toString();//求出整数位数值.
cents = cents.toString();//把小数位转换成字符串,以便求小数位长度.
while(cents.length<cent){//补足小数位到指定的位数.
    cents = "0" + cents;
}

//ymyang 如果 小数位不为0，则显示小数位


if(cent!="0"){
	cents = '.' + cents ;
}else{
	cents = "";
}

if(isThousand == 0) //不需要千分位符.
    return (((sign)?'':'-') + num + cents);
//对整数部分进行千分位格式化.
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + cents);
}
</script>